package com.ncloud.crm.service.impl;

import java.util.List;
import java.util.Map;

import com.ncloud.param.service.SequenceService;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.crm.entity.order.Work;
import com.ncloud.crm.mapper.WorkMapper;
import com.ncloud.crm.service.WorkService;

@Service
public class WorkServiceImpl implements WorkService {
	
	private static final Logger logger = LogManager.getLogger(WorkServiceImpl.class);
	
	@Autowired
	private WorkMapper workMapper;
	@Reference
	private SequenceService sequenceService;


	public JSONArray getId(JSONObject json) {
		return null;
	}

	@Override
	public JSONObject getPk(JSONObject json) {
		if(json!=null && !json.containsKey("workId")){
			throw new UserException(ResultEnum.PARAM_ERROR);
		}
		Work work = workMapper.getWorkById(json.getLong("workId"));
		return JSON.parseObject(JSON.toJSONString(work));
	}

	@Override
	public JSONObject insert(JSONObject json) {

		Work work = JSON.parseObject(json.toJSONString(),Work.class);
		try {
			validation(work);
			workMapper.insertWork(work);
		}catch (RuntimeException e){
			e.printStackTrace();
			throw new UserException(ResultEnum.DATABASE_ERROR);
		}
		return json;
	}
	private void validation(Work account){

	}

	@Override
	public List<Map<String,Object>> getAllWorks(String workState) {
		List<Map<String,Object>> works = workMapper.getAllWorkds(workState);
		return works;
	}

	@Override
	public Work getWorkInfo(Long workId) {
		return workMapper.getWorkById(workId);
	}
	
	public Work getHisWorkInfo(Long workId) {
		return workMapper.getHisWorkById(workId);
	}

	@Override
	public void updateWork(Work work) {
		workMapper.update(work);
	}

	@Override
	public List<Work> getWorksByOrderId(long orderId) {
		return workMapper.getWorksByOrderId(orderId);
	}

	@Override
	public List<Work> getWorksByOrderIdAndState(long orderId, char state) {
		return workMapper.getWorksByOrderIdAndState(orderId,state);
	}

	@Override
	public JSONObject validate(JSONObject json,JSONObject result) {
		JSONObject work = new JSONObject();

		return work;
	}



	@Override
	public int updateWorkState(Work work) {
		return workMapper.updateWorkState(work);
	}

	@Override
	public void moveWork2His(Long orderId) {
		workMapper.insertHisWork(orderId);	
	}

	@Override
	public int deleteByOrderId(Long orderId) {
		return workMapper.deleteByOrderId(orderId);
	}

	@Override
	public List<Map<String, Object>> getWorksByOrder(Long orderId, String orderState) {
		List<Map<String,Object>> works = workMapper.getWorksByOrder(orderId,orderState);
		if(works.size()==0)
			works = workMapper.getHisWorksByOrder(orderId, orderState);
		return works;
	}

	@Override
	public int updateWorkState(Long workId, char workState) {
		return workMapper.updateWorkState2(workId,workState);
	}

	@Override
	public int getNotWorkStateCount(Long orderId) {
		return workMapper.GetNotWorkStateCount(orderId);
	}
}
